package com.iemes.controller.material;

import java.util.List;   
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.BrandFormMap;
import com.iemes.entity.BrandModelFormMap;
import com.iemes.entity.EntryFormMap;
import com.iemes.entity.MaterFromMap;
import com.iemes.entity.QualityFromMap;
import com.iemes.entity.ReceiveFormMap;
import com.iemes.entity.SuppFilleFromMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.MaterMapper;
import com.iemes.mapper.SuppfilleMapper;
import com.iemes.mapper.UserMapper;
import com.iemes.mapper.material.QualityMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/mater/")
@Controller
public class MaterSelController extends BaseController {
	@Inject
	private MaterMapper matertMapper;
	@Inject
	private UserMapper userMapper;
	@Inject
	private QualityMapper qualityMapper;
	@Inject
	private SuppfilleMapper suppMapper;
	
	@RequestMapping("examineMater")
	public String examineOrder(Model model){
		List<MaterFromMap> materFormMaps=matertMapper.findByWhere(new MaterFromMap());
		model.addAttribute("mater", materFormMaps);
		List<UserFormMap> userFormMaps=userMapper.findByWhere(new UserFormMap());
		model.addAttribute("alluser", userFormMaps);
		List<QualityFromMap> qualityFormMap=qualityMapper.findByWhere(new QualityFromMap());
		model.addAttribute("qualitya", qualityFormMap);
		List<BrandModelFormMap> brandModelFormMaps=userMapper.findByWhere(new BrandModelFormMap());
		model.addAttribute("Model", brandModelFormMaps);
		List<BrandFormMap> brandFormMaps=userMapper.findByWhere(new BrandFormMap());
		model.addAttribute("brand", brandFormMaps);
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/material/mater_list";
	}
	
	@ResponseBody
	@RequestMapping("mater_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		MaterFromMap materFormMap = getFormMap(MaterFromMap.class);
		List<Map<String, Object>> materFormMaps=matertMapper.getAllName(materFormMap);
		LuogicPaging(pageNow, pageSize, materFormMaps);
		pageView.setRecords(materFormMaps);
		return pageView;
		
	}
	@RequestMapping("mater_edit")
	public String editUI(Model model) throws Exception 
	{
		String id = getPara("id");
		List<SuppFilleFromMap> suppFormMap=suppMapper.findByWhere(new SuppFilleFromMap());
		model.addAttribute("supp", suppFormMap);
		List<BrandModelFormMap> brandModelFormMaps=userMapper.findByWhere(new BrandModelFormMap());
		model.addAttribute("Model", brandModelFormMaps);
		List<BrandFormMap> brandFormMaps=userMapper.findByWhere(new BrandFormMap());
		model.addAttribute("brand", brandFormMaps);
		List<QualityFromMap> qualityFormMap=qualityMapper.findByWhere(new QualityFromMap());
		model.addAttribute("qualitya", qualityFormMap);
		if(Common.isNotEmpty(id)){
			MaterFromMap materCheckFormMap=baseMapper.findbyFrist("id", id, MaterFromMap.class);
			model.addAttribute("before",materCheckFormMap);
			SuppFilleFromMap suppFormMaps=new SuppFilleFromMap();
			suppFormMaps.put("materialNo", materCheckFormMap.getStr("materialNo"));
			List<SuppFilleFromMap> suppFormMapl=baseMapper.findByNames(suppFormMaps);
			model.addAttribute("supp", suppFormMapl);
		}
		return Common.BACKGROUND_PATH + "/material/mater_edit";
	}
	@ResponseBody
	@RequestMapping(value="mater_editEntity")
	@SystemLog(module="物料数据维护",methods="物料清单-编辑物料")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws Exception {
		MaterFromMap materCheckFormMap=getFormMap(MaterFromMap.class);
		baseMapper.editEntity(materCheckFormMap);
		//先删除
		baseMapper.deleteByAttribute("materialNo","'"+materCheckFormMap.getStr("materialNo")+"'", SuppFilleFromMap.class);
		//再添加
		String productName =request.getParameter("MaterFromMap.productName");
		String unit =request.getParameter("MaterFromMap.unit");
		String materType =request.getParameter("MaterFromMap.materType");
		String qualityId =request.getParameter("MaterFromMap.qualityId");
		String materialNo =request.getParameter("MaterFromMap.materialNo");
		String[] sippNumber1 = request.getParameterValues("sippNumber1");
		String[] suppName = request.getParameterValues("suppName1");
		String[] materialNo1 = request.getParameterValues("materialNo1");
		String[] newprice1 =request.getParameterValues("newprice1");
		if(null != sippNumber1 ){
			for (int i = 0; i < sippNumber1.length; i++) {
				MaterFromMap materFormMap = new MaterFromMap();
				SuppFilleFromMap suppFormMap1 = new SuppFilleFromMap();
				suppFormMap1.put("productName", productName);
				suppFormMap1.put("materialNo", materialNo1[i]);
				suppFormMap1.put("newprice",  Double.parseDouble(newprice1[i]));
				suppFormMap1.put("suppName", suppName[i]);
				suppFormMap1.put("suppNumber", sippNumber1[i]);
				
				materFormMap.put("sippNumber", sippNumber1[i]);
				materFormMap.put("materialNo", materialNo);
				materFormMap.put("productName", productName);
				materFormMap.put("materType", materType);
				materFormMap.put("unit", unit);
				materFormMap.put("qualityId",  Integer.parseInt(qualityId));
				baseMapper.addEntity(materFormMap);
				baseMapper.addEntity(suppFormMap1);
			}
		}
		return "success";
	}
	
	@RequestMapping("mater_adds")
	public String addinfo(Model model) throws Exception{
		List<BrandModelFormMap> brandModelFormMaps=userMapper.findByWhere(new BrandModelFormMap());
		model.addAttribute("Model", brandModelFormMaps);
		List<QualityFromMap> qualityFormMap=qualityMapper.findByWhere(new QualityFromMap());
		model.addAttribute("qualitya", qualityFormMap);
		return Common.BACKGROUND_PATH + "/material/mater_add";
	}

@ResponseBody
@RequestMapping("mater_addEntity")
@SystemLog(module="物料管理",methods="物料清单-新增物料")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		SuppFilleFromMap suppFormMap = getFormMap(SuppFilleFromMap.class);
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		String productName =request.getParameter("MaterFromMap.productName");
		String[] materialNo =request.getParameterValues("MaterFromMap.materialNo");
		String unit =request.getParameter("MaterFromMap.unit");
		String materType =request.getParameter("MaterFromMap.materType");
		String qualityId =request.getParameter("MaterFromMap.qualityId");
		String[] suppNumber1 = request.getParameterValues("suppNumber1");
		String[] suppName = request.getParameterValues("suppName");
		String[] newprice1 =request.getParameterValues("newprice1");
		if(null != suppNumber1 ){
			for (int i = 0; i < suppNumber1.length; i++) {
				MaterFromMap materFormMap = new MaterFromMap();
				SuppFilleFromMap suppFormMap1 = new SuppFilleFromMap();
				suppFormMap1.put("productName", productName);
				suppFormMap1.put("materialNo", materialNo[i]);
				suppFormMap1.put("newprice",  Double.parseDouble(newprice1[i]));
				suppFormMap1.put("suppName", suppName[i]);
				suppFormMap1.put("suppNumber", suppNumber1[i]);
				
				materFormMap.put("sippNumber", suppNumber1[i]);
				materFormMap.put("materialNo", materialNo);
				materFormMap.put("productName", productName);
				materFormMap.put("materType", materType);
				materFormMap.put("unit", unit);
				materFormMap.put("qualityId",  Integer.parseInt(qualityId));
				baseMapper.addEntity(materFormMap);
				baseMapper.addEntity(suppFormMap1);
			}
		}
		return "success";
}

@ResponseBody
@RequestMapping("mater_deleteEntity")
@Transactional(readOnly=false)//需要事务操作必须加入此注解
@SystemLog(module="生产过程控制",methods="pod按钮维护-删除pod按钮")//凡需要处理业务逻辑的.都需要记录操作日志
public String deleteEntity() throws Exception {
	String[] ids = getParaValues("ids");
	for (String id : ids) {
		baseMapper.deleteByAttribute("id", id, MaterFromMap.class);
	}
	return "success";
}



}

